package com.iter.devbox.util;

import java.util.Arrays;
import java.util.Comparator;

/**数组排序工具类。另外还有集合排序工具类Collections*/
public class MyArrays {

	public static void main(String[] args) {
		Student student1 = new Student(20);
		Student student2 = new Student(10);
		Student student3 = new Student(30);
		
		Student[] stdentArry = {student1, student2, student3};
		for (Student student : stdentArry) {
			System.out.println(student.getAge());
		}
		
		System.out.println("......----------......");
		
		//匿名内部类实现自定义对象比较
		Arrays.sort(stdentArry, new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				if (o1.getAge() > o2.getAge()) {
					return 1;
				} else if (o1.getAge() < o2.getAge()) {
					return -1;
				} else {
					return 0;
				}
			}
		});
		
		for (Student student : stdentArry) {
			System.out.println(student.getAge());
		}
	}
}

class Student {
	
	private int age;
	
	public Student(int age) {
		this.age = age;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}